package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.base.mapper.core.SystemUserExtraInfoMapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreUser;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * SystemUserExtraInfoResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 17:35:00
 */
@Slf4j
@Component
public class SystemUserExtraInfoResolveMethod implements ResolveMethod {

    @Resource
    private SystemUserExtraInfoMapper systemUserExtraInfoMapper;

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询数据
        var extraInfo = systemUserExtraInfoMapper.selectById(trigger.getTargetId());
        if (extraInfo == null) {
            throw new BusinessSimpleException("用户的额外信息不存在" + trigger.getTargetId());
        }
        // 执行修改操作
        var wrapper = new LambdaUpdateWrapper<OldCoreUser>()
                .set(OldCoreUser::getUserQq, extraInfo.getQq())
                .set(StringUtils.isNotBlank(extraInfo.getWechat()), OldCoreUser::getUserWeixin, extraInfo.getWechat())
                .eq(OldCoreUser::getUserId, extraInfo.getFkUserId());
        oldCoreUserMapper.update(wrapper);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "system_user_extra_info";
    }

}
